package com.demo2do.fms.admin.web.controller.supplier.reform;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.ReformSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.ReformService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.reform.Reform;

/**
 * 约谈整改查询
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/recipient-reforms")
@MenuSetting("menu-supplier")
public class RecipientReformController {

    @Autowired
    private ReformService reformService;
    
    /**
     * list 
     * 
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-reform-recipient-info")
    public ModelAndView list(@Secure Login login, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/recipient/reform-recipient-list");
        
        int totals = reformService.countByRecipient(login);
        if (totals > 0) {
            
            List<Reform> reforms = reformService.listByRecipient(login, page.with(totals));
            modelAndView.addObject("reforms", reforms);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * search panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/reform/recipient/reform-recipient-search");
        return modelAndView;
    }
    
    /**
     * search
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-supplier-reform-recipient-info")
    public ModelAndView onSearch(ReformSearch search, @Secure Login login, 
                                 HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/recipient/reform-recipient-list");
        
        if (login.isSupplier()) {
            search.setRecipientId(login.getSupplierId());
        }
        
        int totals = reformService.count(search);
        if (totals > 0) {
            
            List<Reform> reforms = reformService.search(search, page.with(totals));
            modelAndView.addObject("reforms", reforms);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * view
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-reform-recipient-info")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/reform/recipient/reform-recipient-view");
        
        Reform reform = reformService.load(Reform.class, id);
        
        modelAndView.addObject("reform", reform);
        
        return modelAndView;
        
    }
    
}
